# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/7/10
# @File   : IC21_OvduCardAndQuasiCnt.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:贷款和（准）贷记卡逾期占比
code:IC21_OvduCardAndQuasiCnt
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def IC21_OvduCardAndQuasiCnt():
    try:
        ICROverdueSummary = getTable('ICROverdueSummary')
        ICRCreditCue002 = getTable('ICRCreditCue002')
        ICRLoancardInfo002Card = getTable('ICRLoancardInfo002*Card')
        ICRStdLoancardInfo002Std = getTable('ICRStdLoancardInfo002*Std')

        if (ICROverdueSummary is None) or (ICRCreditCue002 is None and ICRStdLoancardInfo002Std is None\
            and ICRLoancardInfo002Card is None):
            return u'缺失值'
        elif ICROverdueSummary == [{}] and ICRCreditCue002 == [{}] and ICRStdLoancardInfo002Std == [{}] \
            and ICRLoancardInfo002Card == [{}]:
            return 0
        elif ICROverdueSummary == [{}]:
            return 0
        else:
            #逾期
            count = ICROverdueSummary[0].get('count')
            count2 = ICROverdueSummary[0].get('count2')
            count3 = ICROverdueSummary[0].get('count3')
            if isfloat(count):
                count = float(count)
            else:
                count = None
            if isfloat(count2):
                count2 = float(count2)
            else:
                count2 = None
            if isfloat(count3):
                count3 = float(count3)
            else:
                count3 = None

            if count is None  and count2 is None and count3 is None:
                return u'缺失值'
            else:
                count = count if count != None else 0
                count2 = count2 if count2 != None else 0
                count3 = count3 if count3 != None else 0

            # 笔数
            if ICRCreditCue002 is None:
                houseLoanCount = None
                otherLoanCount = None
                CHouseLoanCount = None
            elif ICRCreditCue002 == [{}]:
                houseLoanCount = 0
                otherLoanCount = 0
                CHouseLoanCount = 0
            else:
                houseLoanCount = ICRCreditCue002[0].get('houseLoanCount')
                otherLoanCount = ICRCreditCue002[0].get('otherLoanCount')
                CHouseLoanCount = ICRCreditCue002[0].get('CHouseLoanCount')

                if isfloat(houseLoanCount):
                    houseLoanCount = float(houseLoanCount)
                else:
                    houseLoanCount = None
                if isfloat(otherLoanCount):
                    otherLoanCount = float(otherLoanCount)
                else:
                    otherLoanCount = None
                if isfloat(CHouseLoanCount):
                    CHouseLoanCount = float(CHouseLoanCount)
                else:
                    CHouseLoanCount = None


            #贷记卡激活数
            loacard_count = 0
            flag = []
            if ICRLoancardInfo002Card is None:
                loacard_count = None
            elif ICRLoancardInfo002Card == [{}]:
                loacard_count = 0
            else:
                for i in ICRLoancardInfo002Card:
                    state = i.get('state')
                    if state not in null_type_list:
                        if state != u'未激活':
                            loacard_count += 1
                    else:
                        flag.append(None)
                if len(flag) == len(ICRLoancardInfo002Card):
                    loacard_count = None
            # 准贷记卡激活数
            stdcard_count = 0
            flag = []
            if ICRStdLoancardInfo002Std is None:
                stdcard_count = None
            elif ICRStdLoancardInfo002Std == [{}]:
                stdcard_count = 0
            else:
                for i in ICRStdLoancardInfo002Std:
                    state = i.get('state')
                    if state not in null_type_list:
                        if state != u'未激活':
                            stdcard_count += 1
                    else:
                        flag.append(None)
                if len(flag) == len(ICRStdLoancardInfo002Std):
                    stdcard_count = None


            if houseLoanCount is None and otherLoanCount is None and CHouseLoanCount is None\
                    and loacard_count is None and stdcard_count is None:
                return u'缺失值'
            else:
                houseLoanCount = houseLoanCount if houseLoanCount != None else 0
                otherLoanCount = otherLoanCount if otherLoanCount != None else 0
                CHouseLoanCount = CHouseLoanCount if CHouseLoanCount != None else 0
                loacard_count = loacard_count if loacard_count != None else 0
                stdcard_count = stdcard_count if stdcard_count != None else 0


            #贷款和（准）贷记卡逾期占比
            result = (count + count2 + count3) / (houseLoanCount + otherLoanCount + CHouseLoanCount + loacard_count + stdcard_count)

            return round(result, 4)
    except:
        return u'缺失值'


result = IC21_OvduCardAndQuasiCnt()
